package com.project.yuepai.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.yuepai.dao.UserBaseMapper;
import com.project.yuepai.dto.UserBaseDto;
import com.project.yuepai.dto.UserRoleDto;
import com.project.yuepai.model.User;
import com.project.yuepai.model.UserRole;
import com.project.yuepai.service.UserBaseService;
import com.project.yuepai.util.RandomUtil;
import com.project.yuepai.util.SHA256Util;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserBaseServiceImpl implements UserBaseService {

    @Resource
    private UserBaseMapper userBaseMapper;

    //手机或邮箱+密码登入检验

    @Override
    public UserBaseDto checkuser(User user) {
        UserBaseDto userBaseDto = userBaseMapper.findUserBaseByPhone(user.getPhone());
        if(userBaseDto==null)
            userBaseDto = userBaseMapper.findUserBaseByEmail(user.getPhone());
        return userBaseDto;
    }


//    @Override
//    public String checkuser(User user){
//        String reStr="";
//        User u=userBaseMapper.findRjuserByUserPhone(user.getPhone());
//        System.out.println("获取到的user:"+u);
//        if(u==null){
//            u=userBaseMapper.findRjuserByUserEmail(user.getPhone());
//            if(u == null){
//                System.out.println("用户不存在！");
//                reStr= "用户不存在！";
//            }
//        }
//        System.out.println("输入密码： "+user.getLoginPwd());
//        if(u!=null){
//            System.out.println("手机： "+u.getPhone());
//            System.out.println("邮箱： "+u.getEmail());
//            System.out.println("数据库密码： "+u.getLoginPwd());
//            if(SHA256Util.getSHA256(user.getLoginPwd()).equals(u.getLoginPwd())){
//                reStr= "成功";
//            }
//            else {
//                reStr= "密码错误！";
//            }
//        }
//        return reStr;
//    }

    @Override
    public boolean createUser(String username,String phone, String pwd) {
        User user = new User();
        user.setUserId(RandomUtil.getUUID());
        user.setPhone(phone);
        user.setUserName(username);
        user.setLoginPwd(SHA256Util.getSHA256(pwd));//SHA256算法加密
        if(userBaseMapper.insertNewUser(user)==1)
            return true;
        return false;
    }

    @Override
    public boolean deleteUser(String userId) {
        if(userBaseMapper.deleteByPrimaryId(userId)==1)
            return true;
        return false;
    }

    @Override
    public boolean updateUser() {
        return false;
    }

    @Override
    public User searchUserById(String userId) {
        return userBaseMapper.selectUserByPrimaryId(userId);
    }

    @Override
    public User searchUserByName(String username) {
        return userBaseMapper.selectUserByName(username);
    }

    @Override
    public User searchUserByPhone(String phone) {
        return userBaseMapper.selectUserByPhone(phone);
    }

    @Override
    public User searchUserByEmail(String email) {
        return userBaseMapper.selectUserByEmail(email);
    }

    @Override
    public boolean judgeUserName(String username) {
        if(userBaseMapper.selectUserByName(username)!=null){
            return true;
        }
        return false;
    }

    @Override
    public boolean updatePwdByEmail(String email, String newPwd) {
        if(userBaseMapper.updatePwdByEmail(email,SHA256Util.getSHA256(newPwd))==0)
            return false;
        return true;
    }

    @Override
    public boolean updatePwdByPhone(String phone, String newPwd) {
        if(userBaseMapper.updatePwdByPhone(phone,SHA256Util.getSHA256(newPwd))==0)
            return false;
        return true;
    }

    @Override
    public PageInfo<UserRoleDto> getUserAllInfos(Integer page, Integer limit, Integer userRole, String nickName, String userPhone) {
        PageHelper.startPage(page,limit);
        List<UserRoleDto> userRoleDtos = new ArrayList<>();
        List<User> users = userBaseMapper.findUsersByConditon(userRole, nickName, userPhone);
        for(User user:users){
            UserRoleDto userRoleDto = new UserRoleDto();
            if(user.getUserRole()==1){
                UserRole userModel = userBaseMapper.findUserModel(user.getUserId());
                userRoleDto.fill(user,userModel);
            } else if(user.getUserRole()==2){
                UserRole userCameraman = userBaseMapper.findUserCameraman(user.getUserId());
                userRoleDto.fill(user,userCameraman);
            }
            userRoleDtos.add(userRoleDto);
        }
        return new PageInfo<>(userRoleDtos);
    }

    @Transactional //添加事务，要么全删除，要么一条都不删
    @Override
    public int deleteUserAndRole(String userId, int userRole, int roleId) {
        if(userRole==1){
            if(userBaseMapper.deleteByPrimaryId(userId)==1 && userBaseMapper.deleteUserModel(roleId)==1 && userBaseMapper.deleteModelOrder(roleId)==1)
                return 1;
        }else if(userRole==2){
            if(userBaseMapper.deleteByPrimaryId(userId)==1 && userBaseMapper.deleteUserCameraman(roleId)==1 && userBaseMapper.deleteCameramanOrder(roleId)==1)
                return 1;
        }
        return 0;
    }
}
